package bankingSystemServer;

import java.util.Vector;

/**
 * stores customer info
 * 
 */
public class UserDatabase {
	private Vector<User> users;

	/**
	 * creates a new CustomerDatabase
	 */
	public UserDatabase() {
		users = new Vector<User>();
	}

	/**
	 * adds a customer to the database
	 * 
	 * @return true if successful, false if customer already exists
	 */
	public boolean add(String name, String password) {
		if (userExists(name)) {
			users.add(new Customer(name, password));
			return true;
		}
		return false;
	}

	/**
	 * determines if a customer with name is already in the database
	 * 
	 * @param name
	 *            the name of the customer
	 * @return true if the customer already exists in the database, false
	 *         otherwise
	 */
	public boolean userExists(String name) {
		if (getUser(name) == null) {
			return false;
		}
		return true;
	}

	public boolean userExists(User checkUser) {
		for (User user : users) {
			if (user.equals(checkUser)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * finds and returns a customer by name
	 * 
	 * @param name
	 * @return true if found, null if not found
	 */
	public User getUser(String name) {
		for (User user : users) {
			if (user.getName().equalsIgnoreCase(name)) {
				return user;
			}
		}
		return null;
	}

	public User getUser(String name, String password) {
		for (User user : users) {
			if (user.equals(new User(name, password))) {
				return user;
			}
		}
		return null;
	}

	public boolean add(User newUser) {
		if (!userExists(newUser)) {
			users.add(newUser);
			return true;
		}
		return false;
	}

	public String toString() {
		String output = "User database:\n";
		for (User user : users) {
			output += user.getName() + ':' + user.getPassword() + '\n';
		}
		return output;
	}
}
